<template>
  <div class="login-container">
    <img class="banner" src="../assets/banner.png">
    <el-card class="login-card">
      <div class="login-title">
        <div>请输入系统名称</div>
        <el-divider></el-divider>
      </div>
      <el-form ref="loginFormRef" :model="user" :rules="rules" status-icon>
        <el-form-item prop="username">
          <el-input
              v-model="user.username"
              placeholder="请输入用户名"
              :disabled="loading"
              prefix-icon="User"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              v-model="user.password"
              type="password"
              placeholder="请输入密码"
              :disabled="loading"
              prefix-icon="Lock"
              show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="form-actions">
          <el-button
              type="primary"
              @click="handleLogin"
              :loading="loading"
              class="login-btn"
          >
            登录
          </el-button>
        </el-form-item>
        <el-alert
            v-if="errorMsg"
            :title="errorMsg"
            type="error"
            show-icon
            :closable="false"
            class="error-alert"
        ></el-alert>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import {ref, reactive} from 'vue'
import {useRouter} from 'vue-router'
import {login} from '@/api/auth'

const router = useRouter()
const loading = ref(false)
const errorMsg = ref('')
const loginFormRef = ref(null)

const user = reactive({
  username: '',
  password: ''
})

const rules = reactive({
  username: [
    {required: true, message: '用户名不能为空', trigger: 'blur'},
    {min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '密码不能为空', trigger: 'blur'},
    {min: 6, message: '密码长度不能少于 6 个字符', trigger: 'blur'}
  ]
})

const handleLogin = async () => {
  // 表单验证
  const valid = await loginFormRef.value.validate().catch(() => false)
  if (!valid) return

  loading.value = true
  errorMsg.value = ''
  try {
    const response = await login(user)
    if (response.code === 200) {
      // 存储登录状态
      sessionStorage.setItem('isLogin', 'true')
      sessionStorage.setItem('username', response.data.loginId)
      //ElMessage.success('登录成功')
      await router.push('/layout')
    } else if (response.code === 401) {
      errorMsg.value = '用户名或密码错误'
    } else {
      errorMsg.value = '登录时发生其他错误'
    }
  } catch (err) {
    errorMsg.value = '登录失败，请重试'
    console.error('Login error:', err)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-image: url("../assets/login.png");
  background-size: cover;
}

.login-card {
  width: 400px;
  padding: 10px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border-radius: 10px;
}

.login-title {
  text-align: center;
  color: #219eff;
  font-size: 28px;
  user-select: none;
}

.form-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.login-btn {
  width: 100%;
}

.error-alert {
  margin-top: 16px;
}
</style>